package org.dromara.pill.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.pill.domain.vo.PillDrugVo;
import org.dromara.pill.domain.bo.PillDrugBo;
import org.dromara.pill.service.IPillDrugService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 药品信息
 *
 * @author Lion Li
 * @date 2023-07-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pill/drug")
public class PillDrugController extends BaseController {

    private final IPillDrugService pillDrugService;

    /**
     * 查询药品信息列表
     */
    @SaCheckPermission("pill:drug:list")
    @GetMapping("/list")
    public TableDataInfo<PillDrugVo> list(PillDrugBo bo, PageQuery pageQuery) {
//        return pillDrugService.queryPageList(bo, pageQuery);
        return pillDrugService.selectPagePillDrugVoList(bo, pageQuery);
    }

    /**
     * 导出药品信息列表
     */
    @SaCheckPermission("pill:drug:export")
    @Log(title = "药品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PillDrugBo bo, HttpServletResponse response) {
        List<PillDrugVo> list = pillDrugService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品信息", PillDrugVo.class, response);
    }

    /**
     * 获取药品信息详细信息
     *
     * @param drugId 主键
     */
    @SaCheckPermission("pill:drug:query")
    @GetMapping("/{drugId}")
    public R<PillDrugVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long drugId) {
        return R.ok(pillDrugService.queryById(drugId));
    }

    /**
     * 新增药品信息
     */
    @SaCheckPermission("pill:drug:add")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PillDrugBo bo) {
        return toAjax(pillDrugService.insertByBo(bo));
    }

    /**
     * 修改药品信息
     */
    @SaCheckPermission("pill:drug:edit")
    @Log(title = "药品信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PillDrugBo bo) {
        return toAjax(pillDrugService.updateByBo(bo));
    }

    /**
     * 删除药品信息
     *
     * @param drugIds 主键串
     */
    @SaCheckPermission("pill:drug:remove")
    @Log(title = "药品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] drugIds) {
        return toAjax(pillDrugService.deleteWithValidByIds(List.of(drugIds), true));
    }
}
